//整理洞察数据insightResult
//categroy可能为输入、过程、结果
var generateCategoryData = function (insightResult, category, portsArray) {
    insightResult = dealInsightData(insightResult);
    return insightResult;
}

function dealInsightData(insightResult) {
    var nodes = [];
    var nodeIndexMapping = {};
    var ports = [];
    for (var i = 0; i < insightResult.length; i++) {
        var insightResultI = insightResult[i];
        var exaNodeLabel = "";
        //端口
        if (/*(TempoMineUtils.isDataSet(insightResultI.portType) || TempoMineUtils.isModel(insightResultI.portType) ||
				TempoMineUtils.isGraph(insightResultI.portType)) && */insightResultI.type != window.constant.DATAMINE_SIGNAL) {
            ports.push(insightResultI);
        } else {
            if (insightResultI.type != window.constant.DATAMINE_SIGNAL) {
                nodes.push(insightResultI);
            } else {
                nodes.splice(0, 0, insightResultI);
            }
            nodeIndexMapping[insightResultI.nodeId] = nodes.length - 1;
        }
    }

    if (ports.length > 0) {
        for (var j = 0; j < ports.length; j++) {
            var nodeid = ports[j].nodeId;
            var curInfo = ports[j];
            var node = nodeIndexMapping[nodeid];
            if (node != undefined) {
                var children = nodes[node].portChildren;
                if (!children) {
                    children = nodes[node].portChildren = [];
                }
                if (nodes[node].locType && nodes[node].locType != 'result') {
                    nodes[node].locType = curInfo.locType;
                }
                children.push(ports[j])
            } else {
                var pnode = {};
                pnode.nodeLabel = ports[j].nodeLabel;
                pnode.portChildren = [];
                pnode.portChildren.push(ports[j]);
                pnode.nodeId = nodeid;
                pnode.nodeType = curInfo.type;
                pnode.nodeConfig = curInfo.nodeType;
                pnode.locType = curInfo.locType;
                pnode.subProcessNodeId = curInfo.subProcessNodeId;
                nodes.push(pnode);
                nodeIndexMapping[pnode.nodeId] = nodes.length - 1;
            }
        }
    }

    var temps = TempoUtils.clone(nodes);
    var temps2 = insightResult;
    if (temps.length > 0) {
        for (var i = 0; i < nodes.length; i++) {
            var item = nodes[i];
            findRoot(item, temps);
        }
        for (var i = 0; i < temps.length; i++) {
            buildTree(temps2, temps[i])
        }

        for (var i = 0; i < temps.length; i++) {
            var n = nodeIndexMapping[temps[i].nodeId];
            if (n == undefined) {
                nodes.push(temps[i]);
                nodeIndexMapping[temps[i].nodeId] = nodes.length - 1;
            }
        }
    }
    return temps;
}

function findRoot(obj, rootArr) {
    for (var i = 0; i < rootArr.length; i++) {
        var curInfo = rootArr[i];
        if (curInfo.subProcessNodeId == obj.nodeId || rootArr[i].subProcessNodeId == obj.nodeId) {
            rootArr.splice(i, 1);
            i--;
        }
    }
}

function buildTree(arr, obj) {
    for (var i = 0; i < arr.length; i++) {
        var a = arr[i];
        var curInfo = a
        if (curInfo.subProcessNodeId == obj.nodeId || a.subProcessNodeId == obj.nodeId) {
            var children = obj.portChildren = obj.portChildren || [];
            if (obj.subProcessNodeId) {
                a.nodeLabel = obj.nodeLabel + '_' + a.nodeLabel;
            }
            children.push(a);
            buildTree(arr, a);
        }
    }
}

var unnecessaryNodeDescNodeTypes = ['data_input', 'data_output'];

function isNecessaryDescNode(node) {
    if (node.port == 'node_default' && unnecessaryNodeDescNodeTypes.indexOf(node.nodeType) != -1) {
        return false;
    }
    return true;
}